﻿using System;
using System.Linq;

namespace Leek.Persistence
{
    public sealed class DomainObjectLinkSet<T, TId>
        where T : DomainObject<T, TId>
    {
        private readonly DomainContext _context;
        private readonly Func<IQueryable<T>, IQueryable<T>> _filter;


        public DomainObjectLinkSet(DomainContext context,
            Func<IQueryable<T>, IQueryable<T>> filter)
        {
            _context = context;
            _filter = filter;
        }


        public IQueryable<T> QueryPersisted()
        {
            var repository = CompositionContainer.GetInstance<IDomainObjectRepository<T, TId>>();
            return _filter(repository.Load(_context, null).AsQueryable()); //TODO: Redesign filtering here!
        }

        public IQueryable<T> QueryNew()
        {
            return _filter(_context.Query<T>().Where(instance => instance.State == DomainObjectState.New));
        }
    }
}
